<?php

// +----------------------------------------------------------------------
// | TAODUDU.COM  [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.taodudu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: ubten <462193409@qq.com> http://www.ubten.com/
// +----------------------------------------------------------------------

namespace Store\Controller;
use Store\Controller\StoreController;
class VirtualController extends StoreController {
    
    function _initialize() {
        parent::_initialize();
        $this->assign('sys','virtual');// 数据集
    }
    //put your code here
    public function index(){
        
        $map['vr_state'] = 1;
        $map['vr_sale'] = 1;
        $pMan = D('vrGoods');
        $count = $pMan->where($map)->order('vr_id desc')->count();//获得满足条件的记录数
        $Page  = new \Think\Page($count,10);//实例化分页类 传入总记录数和每页显示的记录数(10)
        $show  = $Page->show();// 分页显示输出
        $list = $pMan->where($map)->order('vr_id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);// 数据集
        $this->assign('page',$show);// 页码
        $this->assign('action','index');// 页码
        $this->display();
    }
    
    //未上架
    public function shelves(){
        $map['vr_state'] = 1;
        $map['vr_sale'] = 0;
        $pMan = D('vrGoods');
        $count = $pMan->where($map)->order('vr_id desc')->count();//获得满足条件的记录数
        $Page  = new \Think\Page($count,10);//实例化分页类 传入总记录数和每页显示的记录数(10)
        $show  = $Page->show();// 分页显示输出
        $list = $pMan->where($map)->order('vr_id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);// 数据集
        $this->assign('page',$show);// 页码
        $this->assign('action','shelves');// 页码
        $this->display();
    }
    
    //待审核
    public function audit(){
        $map['vr_state'] = 0;
        $map['vr_state'] = 2;
        $pMan = D('vrGoods');
        $count = $pMan->where($map)->order('vr_id desc')->count();//获得满足条件的记录数
        $Page  = new \Think\Page($count,10);//实例化分页类 传入总记录数和每页显示的记录数(10)
        $show  = $Page->show();// 分页显示输出
        $list = $pMan->where($map)->order('vr_id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);// 数据集
        $this->assign('page',$show);// 页码
        $this->assign('action','audit');// 页码
        $this->display();
    }
    
    //回收站
    public function recovery(){
        $map['vr_state'] = 3;
        $pMan = D('vrGoods');
        $count = $pMan->where($map)->order('vr_id desc')->count();//获得满足条件的记录数
        $Page  = new \Think\Page($count,10);//实例化分页类 传入总记录数和每页显示的记录数(10)
        $show  = $Page->show();// 分页显示输出
        $list = $pMan->where($map)->order('vr_id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);// 数据集
        $this->assign('page',$show);// 页码
        $this->assign('action','recovery');// 页码
        $this->display();
    }
    
    //添加虚拟商品
    public function vradd(){
        if(IS_POST){
            //数据处理
            $pics = serialize(I('post.vr_pics'));
            $service = implode(I('post.vr_service'), ',');
            $uselimit = implode(I('post.vr_uselimit'), ',');
            
            $data['vr_title'] = I('post.vr_title');
            $data['vr_cate_id'] = I('post.vr_cate_id');
            $data['vr_cate_name'] = I('post.vr_cate_name');
            $data['vr_thumb'] = I('post.vr_thumb');
            $data['vr_pics'] = $pics;
            $data['vr_endtime'] = strtotime(I('post.vr_endtime'));
            $data['vr_price'] = I('post.vr_price');
            $data['vr_money'] = I('post.vr_money');
            $data['vr_limit'] = I('post.vr_limit');
            $data['vr_num'] = I('post.vr_num');
            $data['vr_jianjie'] = I('post.vr_jianjie');
            $data['vr_usetime'] = I('post.vr_usetime');
            $data['vr_uselimit'] = $uselimit;
            $data['vr_bespeak'] = I('post.vr_bespeak');
            $data['vr_other'] = I('post.vr_other');
            $data['vr_service'] = $service;
            $data['vr_rule'] = I('post.vr_rule');
            $data['vr_desc'] = cJscript($_POST['vr_desc']);
            $data['vr_environ'] = cJscript($_POST['vr_environ']);
            $data['vr_status'] = I('post.vr_status');
            $data['vr_created'] = NOW_TIME;
            $re = M('vrGoods')->add($data);
            if($re){
                $this->success('商品添加成功！',U('audit'));
            }else{
                $this->error('商品添加失败！');
            }
        }
        //获取一级分类
        $cate = M('vrGoodsClass')->where('parent_class_id=0')->select();
        $this->assign('cate',$cate);// 页码
        $this->assign('action','vradd');// 页码
        $this->display();
    }
    
    
    public function vredit(){
        $vr_id = I('id');
        $map['vr_id'] = $vr_id;
        if(IS_POST){
            //数据处理
            $pics = serialize(I('post.vr_pics'));
            $service = implode(I('post.vr_service'), ',');
            $uselimit = implode(I('post.vr_uselimit'), ',');
            
            $data['vr_title'] = I('post.vr_title');
            $data['vr_cate_id'] = I('post.vr_cate_id');
            $data['vr_cate_name'] = I('post.vr_cate_name');
            $data['vr_thumb'] = I('post.vr_thumb');
            $data['vr_pics'] = $pics;
            $data['vr_endtime'] = strtotime(I('post.vr_endtime'));
            $data['vr_price'] = I('post.vr_price');
            $data['vr_money'] = I('post.vr_money');
            $data['vr_limit'] = I('post.vr_limit');
            $data['vr_num'] = I('post.vr_num');
            $data['vr_jianjie'] = I('post.vr_jianjie');
            $data['vr_usetime'] = I('post.vr_usetime');
            $data['vr_uselimit'] = $uselimit;
            $data['vr_bespeak'] = I('post.vr_bespeak');
            $data['vr_other'] = I('post.vr_other');
            $data['vr_service'] = $service;
            $data['vr_rule'] = I('post.vr_rule');
            $data['vr_desc'] = cJscript($_POST['vr_desc']);
            $data['vr_environ'] = cJscript($_POST['vr_environ']);
            $data['vr_status'] = I('post.vr_status');
            $re = M('vrGoods')->where($map)->save($data);
            if($re){
                $this->success('商品修改成功！',U('index'));
            }else{
                $this->error('商品修改失败！');
            }
        }
        $vrGoods = M('vrGoods')->where($map)->find();
        $vrGoods['vr_pics'] = unserialize($vrGoods['vr_pics']);
        //获取一级分类
        $cate = M('vrGoodsClass')->where('parent_class_id=0')->select();
        $this->assign('cate',$cate);// 页码
        $this->assign('vrGoods',$vrGoods);// 页码
        $this->assign('action','vradd');// 页码
        $this->display();
    }
    
    
    //删除并下架
    public function del() {
        $Id = I('post.data', 0, 'intval');
        if (empty($Id)) {
            $this->error('参数不能为空！');
        }
        $map['vr_state'] = 3;
        $map['vr_sale'] = 0;
        if (M('vrGoods')->where('vr_id=' . $Id)->save($map)){
            $this->success('商品已下架并删除！', U('index'));
        }else{
            $this->error('操作失败！');
        }
    }

    //下架
    public function downGoods() {
        $Id = I('post.data', 0, 'intval');
        if (empty($Id)) {
            $this->error('参数不能为空！');
        }
        if (M('vrGoods')->where('vr_id=' . $Id)->save(array('vr_sale'=>0))){
            $this->success('商品已下架！', U('index'));
        } else {
            $this->error('操作失败！');
        }
    }
    
    //上架
    public function upGoods() {
        $Id = I('post.data', 0, 'intval');
        if (empty($Id)) {
            $this->error('参数不能为空！');
        }
        if (M('vrGoods')->where('vr_id=' . $Id)->save(array('vr_sale'=>1))){
            $this->success('商品已下架！', U('index'));
        } else {
            $this->error('操作失败！');
        }
    }
    
    //撤销删除
    public function cannelDel() {
        $Id = I('post.data', 0, 'intval');
        if (empty($Id)) {
            $this->error('参数不能为空！');
        }
        if (M('vrGoods')->where('vr_id=' . $Id)->save(array('vr_state'=>1,'vr_sale'=>0))){
            $this->success('商品已下架！', U('index'));
        } else {
            $this->error('操作失败！');
        }
    }
    
    
    
}
